package com.umsoft.chapter3.controller;

import com.umsoft.chapter3.model.Customer;
import com.umsoft.chapter3.service.CustomerService;
import com.umsoft.smart.framework.Bean.Param;
import com.umsoft.smart.framework.Bean.View;
import com.umsoft.smart.framework.annotation.Action;
import com.umsoft.smart.framework.annotation.Controller;
import com.umsoft.smart.framework.annotation.Inject;
import com.umsoft.smart.framework.util.CastUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
public class CustomerController{

    @Inject
    private CustomerService customerService;

    @Action("get:/customer_list")
    public View findAllCustomer(){
        List<Customer> customerList = customerService.getCustomerList();
        return new View("customer_list.jsp").addModel("customerList",customerList);
    }

    @Action("get:/get_customer")
    public View findCustomer(Param param){
        Map<String,Object> paramMap = param.getMap();
        Long id = CastUtil.castLong(paramMap.get("id"));
        List<Customer> customerList = new ArrayList<>();
        customerList.add(customerService.getCustomer(id));
        return new View("customer_list.jsp").addModel("customerList",customerList);
    }
}
